/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.model.dao.impl;

import com.javacommunity.model.dao.CategoryDAO;
import com.javacommunity.model.entity.Category;
import com.javacommunity.hibernate.HibernateUtil;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Pham
 */
public class CategoryDAOImpl implements CategoryDAO {

    private static CategoryDAOImpl categoryDAO;

    public static CategoryDAOImpl getInstance() {
        if (categoryDAO == null) {
            categoryDAO = new CategoryDAOImpl();
        }
        return categoryDAO;
    }

    private Session session;
    private Transaction ts;

    @Override
    public Category getCategoryById(int catId) {
        session = HibernateUtil.getSessionFactory().openSession();
        Category category = null;
        try {
            ts = session.beginTransaction();
            category = (Category) session.get(Category.class, catId);
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return category;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Category> getCategories(String param) {
        session = HibernateUtil.getSessionFactory().openSession();
        List<Category> categories = null;
        try {
            ts = session.beginTransaction();
            Query query = session.createSQLQuery("CALL sp_GetCategorys(?)").addEntity(Category.class).setParameter(0, param);
            categories = query.list();
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return categories;
    }

    @Override
    public boolean createCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.save(cat);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            session.close();
        }
    }

    @Override
    public boolean updateCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.update(cat);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            session.close();
        }
    }

    @Override
    public boolean deleteCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.delete(cat);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            session.close();
        }
    }

    @Override
    public boolean removeCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            String sql = "UPDATE Category set active = false WHERE categoryId = :categoryId";
            Query query = session.createQuery(sql);
            query.setParameter("categoryId", cat.getCategoryId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            session.close();
        }
    }

    @Override
    public boolean restoreCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            String sql = "UPDATE Category set active = true WHERE categoryId = :categoryId";
            Query query = session.createQuery(sql);
            query.setParameter("categoryId", cat.getCategoryId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            session.close();
        }
    }
}
